// jQuery Effect Library 0.2
// Need Jquery Library Above 1.8.3
// by Aria
// Update 8/28/2013
// 01 Slide set↓
// you need add "var moveBoxType = sth."
var preBtnID = '.left-btn',
    nxtBtnID = '.right-btn',
    imgMark = '.oo',
    boxOuter = '.slide-in',
    speeD = 300;
// 02 POP BOX set↓
var pop = {
    attr: "alt",
    b: "popup"
};
// 03 Scroll
var scrbox = ".scroll",
    globalSpeed = 10000;
$(document).ready(function() {
    // 00 Auto Switch
    function autoSwitch(node, childNodeType, direction, speed) {
        //direction: "left" or "right", [string]
        if (childNodeType == undefined || direction == undefined || speed == undefined) {
            childNodeType = "img";
            direction = "left";
            speed = 500
        }
        var t = $(node);

        t.css({
            'position': 'relative',
            'overflow': 'hidden'
        })
        t.each(function() {
            var x = $(this).children(childNodeType),
                lth = x.length,
                o = $(this);
            x.css({
                'position': 'absolute',
                'display': 'block'
            }).width(o.width()).height(o.height());

            function r(m, n) {
                for (var i = 0; i < lth; i++) {
                    var w = (i + m) * x.width(),
                        h = (i + m) * x.height();
                    if (n == 0) {
                        o.children(childNodeType + ':eq(' + i + ')').css({
                            'left': w,
                            'top': 0
                        })
                    } else if (n == 1) {
                        o.children(childNodeType + ':eq(' + i + ')').css({
                            'left': 0,
                            'top': h
                        })
                    }
                }
            }
            if (direction == "left") {
                x.stop().animate({
                    "left": "-=" + x.width() + "px"
                }, speed, function() {
                    r(0, 0)
                });
                o.children(childNodeType + ':first').appendTo(o);
            } else if (direction == "right") {
                o.children(childNodeType + ':last').prependTo(o);
                r(-1, 0);
                x.stop().animate({
                    "left": "+=" + x.width() + "px"
                }, speed);
            } else if (direction == "bottom") {
                o.children(childNodeType + ':last').prependTo(o);
                r(-1, 1);
                x.stop().animate({
                    "top": "+=" + x.height() + "px"
                }, speed);
            } else if (direction == "top") {
                x.stop().animate({
                    "top": "-=" + x.height() + "px"
                }, speed, function() {
                    r(0, 1)
                });
                o.children(childNodeType + ':first').appendTo(o);
            }
        })
    }
    // 01 Slide, based on Click Event
    $(boxOuter).each(function() {
        var _this = $(this),
            move = $(_this).attr('alt'),
            xR = $(_this).children(imgMark).length,
            moveBoxNum = xR - 1;
        if (move == undefined || moveBoxType == undefined) {
            move = $(_this).width();
            moveBoxType = "img";
        }
        $(_this).css({
            'overflow': 'hidden',
            'position': 'relative'
        }).parent().css({
            'position': 'relative'
        }); //.wrapInner('<div style="position:relative" />') // Firefox BUG
        // if ($(_this).hasClass('cover')) {
        //     $(_this).parent().append('<div class="ooo" style="position:absolute;top:0;left:0;z-index:99999;background:url(images/xxxxxxx.gif)"></div>');
        // }
        var tw = $(_this).width(),
            th = $(_this).height();
        $(_this).siblings('.ooo').width(tw).height(th);

        function al() {
            for (var i = 0; i <= xR; i++) {
                var x = i * move;
                $(_this).children(moveBoxType + ':eq(' + i + ')').css({
                    'left': x,
                    'top': 0
                })
            }
        }

        function ar() {
            for (var i = 0; i <= moveBoxNum; i++) {
                var x = (i - 1) * move;
                $(_this).children(moveBoxType + ':eq(' + i + ')').css({
                    'left': x,
                    'top': 0
                })
            }
        }

        al();
        var spd = _this.attr('speed');
        if (spd == undefined) {
            spd = speeD
        }
        $(_this).siblings(preBtnID).click(function() {
            $(_this).children(imgMark).stop().animate({
                "left": "-=" + move + "px"
            }, spd, al());
            $(_this).children(moveBoxType + ':first').appendTo(_this);
        })
        $(_this).siblings(nxtBtnID).click(function() {
            $(_this).children(moveBoxType + ':last').prependTo(_this);
            ar();
            $(_this).children(imgMark).stop().animate({
                "left": "+=" + move + "px"
            }, spd);
        })
        $(_this).siblings(preBtnID + ',' + nxtBtnID).css({
            'cursor': 'pointer',
            'z-index': '999'
        });
    });
    // (02) POP BOX
    $('a[' + pop.attr + '*=' + pop.b + ']').each(function() {
        var t = $(this);
        t.css('cursor', 'pointer');

        function pBgGen() {
            var o = "popBg",
                h = $('body').height();
            $('body').append('<div class=' + o + ' />');
            $('.' + o + '').css({
                'position': 'absolute',
                'height': h,
                'width': '100%',
                'top': 0,
                'left': 0,
                'z-index': 99999,
                'background': 'url(al.png) repeat'
            })
        }
        t.bind('click', function() {
            pBgGen();

            var a = t.attr(pop.attr),
                b = $('div[' + pop.attr + '*=' + a + 'box]'),
                offset = {
                    t: ($(window).height() - b.height()) / 2,
                    l: ($(window).width() - b.width()) / 2,
                    doc: $(document).scrollTop()
                };
            b.show();
            b.css({
                'position': 'absolute',
                'top': $(document).height(),
                'left': offset.l,
                'z-index': 999999
            }).animate({
                top: offset.t + offset.doc
            }, 500);
            b.children('.close').css('cursor', 'pointer').click(function() {
                b.hide();
                $('.popBg').hide().remove();
            });
        });
    })
    // 03 Scroll
    $(scrbox).each(function() {
        var t = $(this),
            s = t.children('.scroll-in'),
            m = t.children('.scroll-in').children('div');
        m.css('position', 'relative');
        t.children('.scroll-in').css({
            'overflow': 'hidden'
        }).siblings('.u, .d').css({
            'position': 'absolute',
            'display': 'block',
            'cursor': 'pointer'
        });
        var d = t.children('.d'),
            u = t.children('.u');

        function v(x) {
            var m = x.height();
            return m
        }
        d.mousedown(function() {
            var x = m.position().top
            m.animate({
                'top': -v(m) + v(s)
            }, globalSpeed * (1 - ((-x + v(s)) / v(m))), 'linear')
        }).mouseup(function() {
            m.stop(true, false);
        });
        u.mousedown(function() {
            var x = m.position().top
            m.animate({
                'top': 0
            }, globalSpeed * (-x / v(m)), 'linear')
        }).mouseup(function() {
            m.stop(true, false);
        });
    });
});